package org.example.school.controller;

import org.example.school.business.ChatInfo;
import org.example.school.common.Result;
import org.example.school.entity.Users;
import org.example.school.model.dto.ChatMessageDTO;
import org.example.school.service.IUsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/chat")
public class ChatController {

    @Autowired
    private ChatInfo chatInfo;

    @Autowired
    private IUsersService usersService;

    /**
     * 获取老师的联系人列表
     * @param classId 班级ID
     * @return 联系人列表
     */
    @GetMapping("/teacher/contacts")
    public Result<List<ChatInfo.ContactInfo>> getTeacherContacts(@RequestParam Long classId,@RequestParam(required = false) String studentName, @RequestParam(required = false) String studentNumber) {
        try {
            List<ChatInfo.ContactInfo> contacts = chatInfo.getTeacherContacts(classId,studentName, studentNumber);
            return Result.success(contacts);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    /**
     * 获取家长联系人列表
     * @param studentNumber 学生学号
     * @return 联系人列表
     */
    @GetMapping("/parent/contacts")
    public Result<List<ChatInfo.ContactInfo>> getParentContacts(@RequestParam String studentNumber) {
        try {
            List<ChatInfo.ContactInfo> contacts = chatInfo.getParentContacts(studentNumber);
            return Result.success(contacts);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    /**
     * 发送消息
     * @param recipientId 接收方用户ID
     * @param content 消息内容
     * @return 发送结果
     */
    @PostMapping("/send")
    public Result<ChatMessageDTO> sendMessage(
            @RequestParam Long recipientId,
            @RequestParam String content) {
        try {
            ChatMessageDTO message = chatInfo.sendMessage(recipientId, content);
            return Result.success(message);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    /**
     * 获取聊天历史记录
     * @param contactId 联系人ID
     * @param page 页码，默认0
     * @param size 每页大小，默认20
     * @return 消息列表
     */
    @GetMapping("/history")
    public Result<List<ChatMessageDTO>> getChatHistory(
            @RequestParam Long contactId,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "20") int size) {
        try {
            List<ChatMessageDTO> messages = chatInfo.getChatHistory(contactId, page, size);
            return Result.success(messages);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    /**
     * 获取未读消息数
     * @return 未读消息数
     */
    @GetMapping("/unread-count")
    public Result<Long> getUnreadCount() {
        try {
            Long count = chatInfo.getUnreadCount();
            return Result.success(count);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 获取每个联系人的未读消息数
     * @param contactIds 联系人ID列表
     * @return 每个联系人的未读消息数映射
     */
    @PostMapping("/unread-count-by-contacts")
    public Result<Map<Long, Long>> getUnreadCountByContacts(@RequestBody List<Long> contactIds) {
        try {
            Map<Long, Long> unreadCounts = chatInfo.getUnreadCountByContacts(contactIds);
            return Result.success(unreadCounts);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    /**
     * 标记消息为已读
     * @param senderId 发送方ID
     * @return 标记结果
     */
    @PostMapping("/mark-as-read")
    public Result<String> markAsRead(@RequestParam Long senderId) {
        try {
            String result = chatInfo.markAsRead(senderId);
            return Result.success(result);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }
}